/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.sql.Date;
import java.text.ParseException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.JOptionPane;
import model.dao.UserDAO;
import model.dao.service.UserDAOservice;
import model.entities.User;

/**
 *
 * @author Hieu.Nguyen
 */
public class Registration extends HttpServlet {

    UserDAO USD = UserDAO.getInstance();
    private final UserDAOservice USER_SERVICE = UserDAO.getInstance();

    public List<User> getUsers() {
       List<User> users =  USER_SERVICE.getUsers();
       return users;
    }
    
    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
        response.setContentType("text/html;charset=UTF-8");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
         String repassword = request.getParameter("retypePassword");
        String fullname = request.getParameter("fullname");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        int roleID = 1;
        boolean isActived = true;
        String activedCode = "aaa0";
        String tel = request.getParameter("tel");
        String GD = request.getParameter("gender");
        int a = Integer.parseInt(GD);
        boolean gender;
        if (a == 0) {
            gender = false;
        } else {
            gender = true;
        }
        String ngaySinh = request.getParameter("DOB");
        String[] birthDay = ngaySinh.split("-");
        Date ns = util.Support.convertToDate(birthDay[2], birthDay[1], birthDay[0]);

        if(birthDay==null){
            
        }
        
        User user = new User(username, password, email, fullname, gender, ns, address, tel, isActived, roleID, activedCode);

        
       
        user.setUserName(request.getParameter("username"));
        
      
       
        UserDAO userDao = new UserDAO();
            try{
                if(userDao.checkUserRegister(user)){
                response.sendRedirect("regis_Faill.jsp");
                response.sendRedirect("registration.jsp");
            
                } else{
                USD.createrUser(user);
                response.sendRedirect("success.jsp");
                    }
            }
            catch(Exception e){
//                request.getRequestDispatcher("myerror.jsp").forward(request, response);
            }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(Registration.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

        protected void loadUserName (HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException{
            
        }
}
